﻿using Max.PH5_2011A.Common;
using Max.PH5_2011A.Domain;
using Max.PH5_2011A.DTO;
using Max.PH5_2011A.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace Max.PH5_2011A.API.Controllers.Product
{
    [Route("product/spu")]
    [ApiController]
    public class ProductSPUController : BaseController<ProductSpuModel>
    {
        IProductSpuService _serice;
        public ProductSPUController(IProductSpuService serice) : base(serice)
        {
            _serice = serice;
        }

        /*
         * API：方法的返回，是IAactionResult / Task<IActionResult>
         * API中，做三件事：
         * 1、验证参数
         * 2、调用服务
         * 3、统一返回值
         * **/

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("pages")]
        public IActionResult Query(int pageIndex=1,int pageSize=10)
        {
            ResponseResultDto<IQueryable<ProductSpuOutputDto>> result = new();
            //判断参数
            if (pageIndex < 1) pageIndex = 1;
            if(pageSize < 1) pageSize = 10;

            //调用服务数据
            var list = _serice.Query();

            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            //result.Data = list;
            //返回值
            return Ok(result);
        }

        // 2023/05/24
        #region admin

        /// <summary>
        /// 后台管理- 获取SPU列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("admin/pages")]
        public IActionResult AdminGetSpuList(int pageIndex, int pageSize)
        {
            ResponseResultDto<dynamic> result = new();
            var list = _serice.Admin_GetSpuList(pageIndex,pageSize,out int total);
            result.Data = new { list, total };
            return Ok(result);
        }
        /// <summary>
        /// 后台管理- 根据SPUID获取SKU
        /// </summary>
        /// <param name="spuId"></param>
        /// <returns></returns>
        [HttpGet("admin/skulist")]
        public IActionResult AdminGetSkuListBySpuId(string spuId)
        {
            ResponseResultDto<dynamic> result = new();
            var skuList = _serice.Admin_GetSkuListBySpuId(spuId);
            var specList = _serice.Admin_GetSpuSpecBySpuId(spuId);
            result.Data = new { skuList, specList };
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost("admin/upload")]
        public async Task<IActionResult> Admin_UploadSkuImage(string code)
        {
            ResponseResultDto<dynamic> response = new();

            var files = Request.Form.Files;
            var result = await FileHelper.FileUploadAsnyc(files, $"spu/{code}");
            if (result.Item1)
            {
                //string hostName = Dns.GetHostName();
                //Dns.GetHostAddresses(hostName)
                //    ;
                
                response.Data = result.Item3;
            }
            else
            {
                response.Code = ResponseStateCode.FAILED;
                response.Message = result.Item2;
            }
            return Ok(response);
        }

        #endregion
    }

}
